#!/bin/bash
#############################  AUTOARCHIVE  ##################################
# Script Name      :  autoarchive
# Project          :  SuperServer  https://code.google.com/p/superserver/
# Authors          :  Copyright (c) 2012 TWConnor twconnor@gmail.com
# Lastest Revision :  02/26/2013 20:39:53  
# Created On       :  07.05.2011 11:36:12
# Version          :  0.0.1.0
# Description      :  A backup script that provides backup to a local drive, #                     removable drive, rsync server, dropbox folder with or #                     without compression.
# Prerequisites    :  Ubuntu 12.04, Ubuntu 12.10, ping, date, rsync  version 3.0.9
#                     OPTIONALLY: iWatch 0.2.2, gzip 1.5
# Notes            :  Requires superuser execution. 
#                     Must use full directory path.
#                     Do NOT use variables like '$USER' as it will fail.
#
################################# OPTIONS ####################################
#
# gzip all files individually in the incrementalbackup directory.
COMPRESS=YES 
# /home/tim/backups
DESTINATION=/home/tim/backups  
#
DODROPBOX=YES
#
DOLOCAL=YES
#
DROPBOXFOLDER=/home/tim/Dropbox #
#
KEEPDAYS=30 # 30 The number of days to keep incremental backups.
#
LOCALDESTINATION=/home/tim/backups
#
REMOVABLEDRIVE= # 
# 192.168.0.XXX::rsyncsrv
RSYNCSERVER= # 192.168.0.XXX::rsyncsrv
#
SOURCE=/home/tim/Documents #/home/user/Documents or --files-from='to_backup.txt'
#
TEMPFOLDER=/tmp
#
WATCHSOURCE=YES 
#

#
###################### DO NOT EDIT BELOW THIS LINE ###########################
#
DAY=$(date +"%d") #
DELETEDATE=$(date --date="-$KEEPDAYS days" +"%Y/%B/%d") #
DIRDATE=$(date +"%Y/%B/%d") #
LOCKFILE=/tmp/`basename $0`.lock #
LOG=$SOURCE/backuplog.txt #
MONTH=$(date +"%B") #
NAME=`basename $0` #
NOW=$(date +"-%F-%T") #
TIMESTAMP=$(date +"%D-%T") #
VERSION=0.0.1.0 #
YEAR=$(date +"%Y") #
#
set -e
set -x
trap 'rm $LOCKFILE' EXIT
#
# Force Single Instance
function singleinstance () {
        (set -C; : > $LOCKFILE) 2> /dev/null
        if [ $? != "0" ]; then
            echo "Lock File exists - exiting"
            exit 1
        fi
}
# Require root priviledge.
function requireroot () {
        if [ $USER != root ]; then
            echo -e "ERROR: Must be root"
                        echo -e "Attempting sudo/su"
                        sudo $0 $@ || su -c $0 $@
                exit 0
        fi
}
# Test if internet connection is working
function netup () {
    /bin/ping -c 1 google.com > /dev/null 2>&1 && return $YES || echo "Network NOT up" && sleep 120 && netup
}
## Do Update and Upgrade
function doupdate () {
        if [ -e /usr/sbin/apt-fast ]; then 
        /usr/sbin/apt-fast -qq -y update && /usr/sbin/apt-fast -qq -y dist-upgrade  && [ -f /var/run/reboot-required ] && /sbin/shutdown -r +30
        else 
        /usr/bin/apt-get -qq -y update && /usr/bin/apt-get -qq -y dist-upgrade  && [ -f /var/run/reboot-required ] && /sbin/shutdown -r +30
        fi
}
# Log entry
function dodologentry () {
    echo $TIMESTAMP" $1" >> $LOG
}
# Sync
function sync () {
    /usr/bin/rsync -aub --backup-dir=./incrementalbackup/$DIRDATE --suffix=$NOW $SOURCE $DESTINATION
}
# Cleanup
function cleanup () {
    for i in {0..32}
        do
        rm -rf $DESTINATION/incrementalbackup/$DELETEDATE && KEEPDAYS=$(($KEEPDAYS + 1)) DELETEDATE=$(date --date="-$KEEPDAYS days" +"%Y/%B/%d")
    done
}
#
#
# HELP -h
function dohelp (){
  echo "\
  "$NAME Version: $VERSION"
  Usage: `basename $0` [-h -o -s -u]
-f, --flag; Set a flag
-o, --opt; Set an option with argument (default: $OPT)
-v, --verbose; Enable verbose output (include multiple times for more
; verbosity, e.g. -vvv)
" | column -t -s ";"
}

if [ "$1" = "-h" ]; then
  dohelp
  exit 0
fi
# Cleanup -C
if [ "$1" = "-C" ]; then
    requireroot
    cleanup && dologentry "Manual Cleanup Performed"
  exit 0
fi
# Overide -O
if [ "$1" = "-O" ]; then
    dologentry "autobackup Started in Overide Mode"
    requireroot &&  dologentry "autobackup is running as root"
    netup  &&  dologentry "Connection to internet confirmed"
    sync && dologentry "Backup Completed in Overide Mode"
    cleanup && dologentry "Cleanup completed"
  exit 0
fi
# SETUP -s
# UPDATE -u
singleinstance
requireroot
netup
dologentry "Backup Started"
sync
dologentry "Backup Completed"
cleanup
#
exit 0

